package com.wut.clock.controller;

import com.wut.clock.aop.log.BusinessType;
import com.wut.clock.aop.log.Log;
import com.wut.clock.common.api.CommonPage;
import com.wut.clock.common.api.CommonResult;
import com.wut.clock.entity.SysRole;
import com.wut.clock.service.IMenuService;
import com.wut.clock.service.IRoleService;
import com.wut.clock.vo.admin.MenuVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author ZYH
 * @unit D9Lab
 * @description 角色管理
 * @date 2022/5/30 16:01
 */
@Api(value = "角色管理",tags = {"角色管理"})
@RestController
@RequestMapping("/sys/role")
@RequiredArgsConstructor
@Slf4j
public class RoleController {

    private final IRoleService roleService;

    private final IMenuService menuService;

    @ApiOperation("增加角色")
    @PostMapping("/addOneRole")
    @RequiresPermissions("sys:role:add")
    @Log(title = "添加角色",businessType = BusinessType.INSERT)
    public CommonResult addOneRole(@Valid @RequestBody SysRole role){
        int result = roleService.insertOneRole(role);
        if (result > 0) {
            return CommonResult.success("添加角色成功");
        }
        return CommonResult.failed("添加角色失败");
    }

    @ApiOperation("根据角色id删除角色")
    @PostMapping("/deleteOneRoleByRoleId/{roleId}")
    @RequiresPermissions("sys:role:delete")
    @Log(title = "删除角色",businessType = BusinessType.DELETE)
    public CommonResult deleteOneRoleByRoleId(@NotNull @PathVariable Integer roleId){
        log.info("roleId={}",roleId);
        int deleteResult = roleService.deleteRole(roleId);
        if (deleteResult > 0) {
            return CommonResult.success("删除角色成功");
        }
        return CommonResult.failed("删除角色失败");
    }

    @ApiOperation("更新角色")
    @PostMapping("/updateRole")
    @RequiresPermissions("sys:role:update")
    @Log(title = "更新角色",businessType = BusinessType.UPDATE)
    public CommonResult updateRole(@Valid @RequestBody SysRole role){
        int result = roleService.updateRole(role);
        if (result > 0) {
            return CommonResult.success("更新角色成功");
        }
        return CommonResult.failed("角色更新失败");
    }
    @ApiOperation("获取所有角色")
    @GetMapping("/getRoleList")
    @RequiresPermissions("sys:role:query")
    public CommonResult getAdminList(@RequestParam(value="pageNum",defaultValue = "1") Integer pageNum,
                                     @RequestParam(value="pageSize",defaultValue ="5") Integer pageSize,
                                     @RequestParam(required = false) String name,
                                     @RequestParam(required = false) String nickName){
        CommonPage<SysRole> roleList = roleService.getRoleList(pageNum, pageSize,name,nickName);
        if (roleList != null) {
            return CommonResult.success(roleList);
        }
        return CommonResult.failed("获取角色列表失败");
    }

    @ApiOperation("根据角色获取所有菜单")
    @GetMapping("/getMenuVosByRoleId")
    public CommonResult getMenuVosByRoleId(@NotNull @RequestParam Integer roleId){
        List<MenuVo> menuVos = menuService.listMenuVosByRoleId(roleId);
        if (!menuVos.isEmpty()) {
            return CommonResult.success(menuVos,"获取角色菜单成功");
        }
        return CommonResult.failed("获取角色菜单失败");
    }

    @ApiOperation("指定角色菜单")
    @PostMapping("/assignMenuRole")
    @RequiresPermissions("sys:role:assignMenu")
    @Log(title = "分配角色菜单",businessType = BusinessType.OTHER)
    public CommonResult assignMenuRole(@NonNull @RequestParam Integer roleId,
                                       @NotBlank @RequestParam String menuIdString){
        int res = roleService.assignMenuRole(roleId,menuIdString);
        if (res <= 0) {
            return CommonResult.failed("批量插入失败，数据库插入有问题");
        }
        return CommonResult.success("指定角色菜单成功");
    }

}
